#include "lasercameraconfigview.h"
#include "ui_lasercameraconfigview.h"
#include <QTimer>
#include "../CommunicateDelegate/communicatedelegate.h"
#include <QMessageBox>
#define D_UPDATE_PERIOD 100
extern GlobalData *g_globalData;

LaserCameraConfigView::LaserCameraConfigView(int styleId,
                                             CommunicateNormalDelegate *communicateNormalThreadIn,
                                              QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LaserCameraConfigView)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    setStyleSheet("background-color:white;");
    communicateNormalThread=communicateNormalThreadIn;

    move(0,0);
    initialDisplayStyle(styleId);
//    setGeometry(70,128,460,595);
    hide();

    //
    ui->position_tableWidget->setColumnCount(9);
    ui->position_tableWidget->setRowCount(5);
    QStringList strHead;
    strHead << tr("X") << tr("Y") << tr("Z") << tr("W") << tr("P") << tr("R")<< tr("cX") << tr("cY") << tr("cZ");
    ui->position_tableWidget->setHorizontalHeaderLabels(strHead);
    ui->position_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择模式，选择单行
    ui->position_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    QStringList hStrList;
    for(int i = 0; i < 5; i++)
    {
        hStrList << tr("P")+QString::number(i+1);
        ui->position_tableWidget->setVerticalHeaderLabels(hStrList);
    }

    timer=new QTimer;
    connect( timer, SIGNAL(timeout()), this, SLOT(timeOutSlot()) );
}

void LaserCameraConfigView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);

        ui->forward_Btn->setGeometry(500,400,80,40);

        ui->scrollArea->setGeometry(0,0,470,440);

        break;
    default:
        break;
    }
}

LaserCameraConfigView::~LaserCameraConfigView()
{
    delete ui;
}

void LaserCameraConfigView::showView_slot()
{
    //获取相机配置
    LaserCameraConfigParameter configInfo;
    communicateNormalThread->getLaserCameraConfigParameter(configInfo);
    ui->lineEdit_cameraIp->setText(QString::fromStdString(configInfo.ip));
    ui->lineEdit_cameraPort->setText(QString::number(configInfo.port));
    ui->comboBox_protocol->setCurrentIndex(configInfo.protocol);
    ui->lineEdit_xRatio->setText(QString::number(configInfo.xRatio));
    ui->lineEdit_zRatio->setText(QString::number(configInfo.zRatio));
    //显示标定数据

    communicateNormalThread->getLaserCameraFrameInfo(frameInfo);
    if(6==frameInfo.cameraTransfer.size())
    {
        ui->X_lineEdit->setText(QString::number(frameInfo.cameraTransfer[0]));
        ui->Y_lineEdit->setText(QString::number(frameInfo.cameraTransfer[1]));
        ui->Z_lineEdit->setText(QString::number(frameInfo.cameraTransfer[2]));
        ui->W_lineEdit->setText(QString::number(frameInfo.cameraTransfer[3]));
        ui->P_lineEdit->setText(QString::number(frameInfo.cameraTransfer[4]));
        ui->R_lineEdit->setText(QString::number(frameInfo.cameraTransfer[5]));
        ui->error_lineEdit->setText(QString::number(frameInfo.maxError));
        if(5==frameInfo.tcpPointList.size()&& 5==frameInfo.cameraPointList.size())
        {
            for(int i = 0; i < 5; i ++)
            {
                //world
                for(int j = 0; j < 6; j ++)
                {
                        QString data = QString::number(frameInfo.tcpPointList[i].positionValue[j],'f',2);
                        ui->position_tableWidget->setItem(i, j, new QTableWidgetItem(data));
                }
                //camera
                for(int j = 0; j < 3; j ++)
                {
                        QString data = QString::number(frameInfo.cameraPointList[i].positionValue[j],'f',2);
                        ui->position_tableWidget->setItem(i, j+6, new QTableWidgetItem(data));
                }
            }

        }
        else
        {
            frameInfo.tcpPointList.resize(5);
            frameInfo.cameraPointList.resize(5);
        }

    }
    else
    {
        frameInfo.tcpPointList.resize(5);
        frameInfo.cameraPointList.resize(5);
    }

    show();
    timer->start(D_UPDATE_PERIOD);
}

void LaserCameraConfigView::timeOutSlot()
{
    //显示相机状态数据 m

    communicateNormalThread->getLaserCameraStatus(cameraStatusInfo);
    ui->lineEdit_cameraConnectStatus->setText(QString::number(cameraStatusInfo.connectStatus));
    ui->lineEdit_currentJobId->setText(QString::number(cameraStatusInfo.jobId));
    ui->lineEdit_isFinded->setText(QString::number(cameraStatusInfo.isFind));
    ui->lineEdit_laserIsOn->setText(QString::number(cameraStatusInfo.laserStatus));
    ui->lineEdit_originX->setText(QString::number(cameraStatusInfo.origin_x));
    ui->lineEdit_originZ->setText(QString::number(cameraStatusInfo.origin_z));
    ui->lineEdit_realX->setText(QString::number(cameraStatusInfo.real_x));
    ui->lineEdit_realZ->setText(QString::number(cameraStatusInfo.real_z));
    ui->lineEdit_worldX->setText(QString::number(cameraStatusInfo.world_x));
    ui->lineEdit_worldY->setText(QString::number(cameraStatusInfo.world_y));
    ui->lineEdit_worldZ->setText(QString::number(cameraStatusInfo.world_z));
    ui->lineEdit_readSucessCount->setText(QString::number(cameraStatusInfo.regReadSucessCount));
}

void LaserCameraConfigView::on_forward_Btn_clicked()
{
    hide();
    timer->stop();
}

void LaserCameraConfigView::on_pushButton_reconnect_clicked()
{
    int tmpReturn;
    communicateNormalThread->reconnectLaserCamera(tmpReturn);
}

void LaserCameraConfigView::on_pushButton_modifyParameter_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否修改相机参数！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }

    LaserCameraConfigParameter parameter;
    parameter.ip=ui->lineEdit_cameraIp->text().toStdString();
    parameter.port=ui->lineEdit_cameraPort->text().toInt();
    parameter.protocol=ui->comboBox_protocol->currentIndex();
    parameter.xRatio=ui->lineEdit_xRatio->text().toDouble();
    parameter.zRatio=ui->lineEdit_zRatio->text().toDouble();
    int returnFlag;
    communicateNormalThread->setLaserCameraConfigParameter(parameter,returnFlag);
    QMessageBox::information(this,"提示", "修改成功！", QMessageBox::Ok );
}

void LaserCameraConfigView::on_pushButton_setWork_clicked()
{
    int returnFlag;
    communicateNormalThread->setLaserCameraWork(true,returnFlag);
    QMessageBox::information(this,"提示", "打开激光！", QMessageBox::Ok );
}

void LaserCameraConfigView::on_pushButton_setStopWork_clicked()
{
    int returnFlag;
    communicateNormalThread->setLaserCameraWork(false,returnFlag);
    QMessageBox::information(this,"提示", "关闭激光！", QMessageBox::Ok );
}

void LaserCameraConfigView::on_button_recordPoint_clicked()
{
    int pointIndex = ui->position_tableWidget->currentRow();
    if(pointIndex<0)
    {
        QMessageBox::information(this,"提示", "请先选择点对应行！", QMessageBox::Ok );
    }
    PointPro tmpWorldPoint;
//    g_globalData->lock();
    g_globalData->getCurrentArmPosition_decart(tmpWorldPoint);
//    tmpWorldPoint=g_globalData->currentPositionWorld;
//    g_globalData->unlock();
    frameInfo.tcpPointList[pointIndex]=tmpWorldPoint;
    PointPro tmpCameraPoint;
    tmpCameraPoint.positionValue.resize(6);
    tmpCameraPoint.positionValue[0]=cameraStatusInfo.real_x;
    tmpCameraPoint.positionValue[1]=cameraStatusInfo.real_z;
    tmpCameraPoint.positionValue[2]=0;
    frameInfo.cameraPointList[pointIndex]=tmpCameraPoint;

    //world
    if(6==tmpWorldPoint.positionValue.size())
    {
        for(int j = 0; j < 6; j ++)
        {
                QString data = QString::number(frameInfo.tcpPointList[pointIndex].positionValue[j],'f',2);
                ui->position_tableWidget->setItem(pointIndex, j, new QTableWidgetItem(data));
        }
    }

    //camera
    if(6==tmpCameraPoint.positionValue.size())
    {
        for(int j = 0; j < 3; j ++)
        {
                QString data = QString::number(frameInfo.cameraPointList[pointIndex].positionValue[j],'f',2);
                ui->position_tableWidget->setItem(pointIndex, j+6, new QTableWidgetItem(data));
        }
    }


    QMessageBox::information(this,"提示", "记录点成功！", QMessageBox::Ok );

}

void LaserCameraConfigView::on_button_calculate_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否标定相机安装位置！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }

    LaserCameraCalibrateResult resultOut;
    communicateNormalThread->calculateLaserCameraFrame(frameInfo.tcpPointList,frameInfo.cameraPointList,
                                                       resultOut);
    if(6==resultOut.cameraTransfer.size())
    {
        ui->X_lineEdit->setText(QString::number(resultOut.cameraTransfer[0]));
        ui->Y_lineEdit->setText(QString::number(resultOut.cameraTransfer[1]));
        ui->Z_lineEdit->setText(QString::number(resultOut.cameraTransfer[2]));
        ui->W_lineEdit->setText(QString::number(resultOut.cameraTransfer[3]));
        ui->P_lineEdit->setText(QString::number(resultOut.cameraTransfer[4]));
        ui->R_lineEdit->setText(QString::number(resultOut.cameraTransfer[5]));
        ui->error_lineEdit->setText(QString::number(resultOut.maxError));
        QMessageBox::information(this,"提示", "标定成功！", QMessageBox::Ok );
    }
    else
    {
        QMessageBox::information(this,"提示", "计算失败,请重新设置点！", QMessageBox::Ok );
    }


}

void LaserCameraConfigView::on_pushButton_setJobId_clicked()
{
    int returnFlag;
    communicateNormalThread->setLaserCameraJobId(ui->lineEdit_jobId->text().toInt(),returnFlag);
    QMessageBox::information(this,"提示", "job切换成功", QMessageBox::Ok );
}



void LaserCameraConfigView::on_moveToPoint_Btn_pressed()
{
    MoveParameter sendPoint;
    sendPoint.pointType = 0;    //普通点
    sendPoint.moveType = 0;     //默认关节方式移动


    g_globalData->getCurrentArmPosition_decart(sendPoint.targetPoint);

    sendPoint.targetPoint.positionValue[0]=cameraStatusInfo.world_x;
    sendPoint.targetPoint.positionValue[1]=cameraStatusInfo.world_y;
    sendPoint.targetPoint.positionValue[2]=cameraStatusInfo.world_z;


    emit moveToCoordinatePointPress(sendPoint);
}

void LaserCameraConfigView::on_moveToPoint_Btn_released()
{
    emit moveToCoordinatePointRelease();
}

void LaserCameraConfigView::on_button_moveToSetPoint_pressed()
{
    MoveParameter sendPoint;
    sendPoint.pointType = 0;    //普通点
    sendPoint.moveType = 0;     //默认关节方式移动

    int pointIndex = ui->position_tableWidget->currentRow();
    if(pointIndex<0)
    {
        qDebug()<<"no point - point index is -1";
        return;
    }

    sendPoint.targetPoint=frameInfo.tcpPointList[pointIndex];
    emit moveToCoordinatePointPress(sendPoint);

}

void LaserCameraConfigView::on_button_moveToSetPoint_released()
{
    emit moveToCoordinatePointRelease();
}



void LaserCameraConfigView::on_pushButton_modifyTrackConfig_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否修改跟踪参数！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }

    struct LaserTrackConfig paraIn;
    paraIn.filterCount=ui->lineEdit_filterCount->text().toInt();

    int returnFlag;
    communicateNormalThread->setLaserTrackConfigParameter( paraIn,returnFlag);
    QMessageBox::information(this,"提示", "修改成功！", QMessageBox::Ok );
}

void LaserCameraConfigView::on_pushButton_modifyBufferMove_clicked()
{
    QMessageBox message(QMessageBox::Question, "提示", "是否修改缓存轨迹参数！"
                        , QMessageBox::Yes|QMessageBox::No , NULL);
    if(message.exec() == QMessageBox::No)
    {
        return;
    }

    struct BufferMoveConfig paraIn;
    paraIn.allowMaxGap=ui->lineEdit_allowMaxGap->text().toDouble();

    int returnFlag;
    communicateNormalThread->setLaserCameraBufferMoveConfigParameter( paraIn,returnFlag);
    QMessageBox::information(this,"提示", "修改成功！", QMessageBox::Ok );
}

void LaserCameraConfigView::on_tabWidget_tabBarClicked(int index)
{
    switch(index)
    {
    case 3:
    {
        struct LaserTrackConfig paraOut;
        communicateNormalThread->getLaserTrackConfigParameter(paraOut);
        ui->lineEdit_filterCount->setText(QString::number(paraOut.filterCount));
        break;
    }

    case 4:
    {
        struct BufferMoveConfig bufferparaOut;
        communicateNormalThread->getLaserCameraBufferMoveConfigParameter(bufferparaOut);
        ui->lineEdit_allowMaxGap->setText(QString::number(bufferparaOut.allowMaxGap));
        break;
    }

    }
}
